<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_payment_management;
use cmf\lib\Plugin;
use think\Config;
use think\db;

class QbtPaymentManagementPlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtPaymentManagement',
        'title'       => '支付管理',
        'description' => '千佰特支付管理',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com',
    ];


    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        //支付方式列表
        Db::query("DROP TABLE IF EXISTS `{$config['prefix']}payment_management`");
        Db::query("
        CREATE TABLE `{$config['prefix']}payment_management`  (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `config_json` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '配置',
              PRIMARY KEY (`id`) USING BTREE
            ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
        ");
        Db::query("INSERT INTO `{$config['prefix']}payment_management` VALUES (1, '{\"pay_name\":\"微信支付\",\"appid\":\"\",\"mchid\":\"\",\"key\":\"\",\"appsecret\":\"\"}')");
        Db::query("INSERT INTO `{$config['prefix']}payment_management` VALUES (2, '{\"pay_name\":\"支付宝支付\",\"appid\":\"\",\"merchant_private_key\":\"\",\"alipay_public_key\":\"\",\"sign_type\":\"\"}')");

        //支付记录
        Db::query("DROP TABLE IF EXISTS `{$config['prefix']}payment_log`");
        Db::query("
        CREATE TABLE `{$config['prefix']}payment_log`  (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `transaction_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '微信订单号',
              `money` decimal(10,2)  DEFAULT 0 COMMENT '订单金额',
              `order_no` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '商户单号',
              `pay_type` tinyint(1) NULL DEFAULT NULL COMMENT '支付方式',
              `trade_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '交易类型',
              `pay_time` int(10)  DEFAULT 0 COMMENT '付款时间',
              PRIMARY KEY (`id`) USING BTREE
            ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
        ");

        $info1 = Db::name('auth_rule')->where([
            "name"  => 'plugin/QbtPaymentManagement/AdminIndex',
            "app"   => 'plugin/QbtPaymentManagement',
            "type"  => "admin_url", //type 1-admin rule;2-user rule
        ])->count();
        if (empty($info1)) {
            Db::name('auth_rule')->insert([
                "name"  => 'plugin/QbtPaymentManagement/AdminIndex',
                "app"   => 'plugin/QbtPaymentManagement',
                "type"  => "admin_url", //type 1-admin rule;2-user rule
                "title" => '支付管理',
                'param' => '',
            ]);
        }
        $info2 = Db::name('auth_rule')->where([
            "name"  => 'plugin/QbtPaymentManagement/AdminIndex/index',
            "app"   => 'plugin/QbtPaymentManagement',
            "type"  => "admin_url", //type 1-admin rule;2-user rule
        ])->count();
        if (empty($info2)) {
            Db::name('auth_rule')->insert([
                "name"  => 'plugin/QbtPaymentManagement/AdminIndex/index',
                "app"   => 'plugin/QbtPaymentManagement',
                "type"  => "admin_url", //type 1-admin rule;2-user rule
                "title" => '支付方式',
                'param' => '',
            ]);
        }
        $info3 = Db::name('auth_rule')->where([
            "name"  => 'plugin/QbtPaymentManagement/AdminIndex/index1',
            "app"   => 'plugin/QbtPaymentManagement',
            "type"  => "admin_url", //type 1-admin rule;2-user rule
        ])->count();
        if (empty($info3)) {
            Db::name('auth_rule')->insert([
                "name"  => 'plugin/QbtPaymentManagement/AdminIndex/index1',
                "app"   => 'plugin/QbtPaymentManagement',
                "type"  => "admin_url", //type 1-admin rule;2-user rule
                "title" => '支付日志',
                'param' => '',
            ]);
        }

        $data['parent_id'] = 0;
        $data['type'] = 0;
        $data['status'] = 1;
        $data['icon'] = 'user';
        $data['app'] = 'plugin/QbtPaymentManagement';
        $data['controller'] = 'AdminIndex';
        $data['action'] = 'index';
        $data['name'] = '支付管理';
        $data['remark'] = '千佰特支付管理';
        $id = Db::name('admin_menu')->insertGetId($data);
        $data1['parent_id'] = $id;
        $data1['type'] = 0;
        $data1['status'] = 1;
        $data1['app'] = 'plugin/QbtPaymentManagement';
        $data1['controller'] = 'AdminIndex';
        $data1['action'] = 'index';
        $data1['name'] = '支付方式';
        $data1['remark'] = '千佰特支付管理';
        Db::name('admin_menu')->insert($data1);
        $data2['parent_id'] = $id;
        $data2['type'] = 0;
        $data2['status'] = 1;
        $data2['app'] = 'plugin/QbtPaymentManagement';
        $data2['controller'] = 'AdminIndex';
        $data2['action'] = 'index1';
        $data2['name'] = '支付日志';
        $data2['remark'] = '千佰特支付管理';
        Db::name('admin_menu')->insert($data2);

        return true;
    }

    // 插件卸载
    public function uninstall()
    {
        $config = Config::get('database');
        Db::query("DROP TABLE `{$config['prefix']}payment_management`");
        Db::query("DROP TABLE `{$config['prefix']}payment_log`");
        return true;
    }
}